package dao.impl;






import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;


import model.AsistentSocial;
import dao.AsistentDao;
import dao.DaoException;

public class AsistentDaoImpl extends Dao implements AsistentDao{

	@Override
	public void addOrUpdateAsistent(AsistentSocial asistent) {
		hibernateTemplate.saveOrUpdate(asistent);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<AsistentSocial> getAllAsistenti() {
		List<AsistentSocial> res= hibernateTemplate.loadAll(AsistentSocial.class);
		return new HashSet<AsistentSocial>(res);
	}

	@Override
	public void removeAsistent(int id) throws DaoException {
		hibernateTemplate.delete(this.getAsistentByID(id));
		
	}

	@Override
	public AsistentSocial getAsistentByID(int id) throws DaoException {
		DetachedCriteria crit = DetachedCriteria.forClass(AsistentSocial.class).add(Restrictions.eq("id", id));
        List res = hibernateTemplate.findByCriteria(crit);
        if (res.size() == 0) {
            throw new DaoException("Nu s-a gasit nici un asistent cu acest id");
        }
       
        return (AsistentSocial) res.get(0);
	}

	@Override
	public int getMaxId() {
		List res= hibernateTemplate.find("select max(userId) from AsistentSocial");
		if( res.get(0) == null || res.isEmpty() ) return 0;
		return Integer.parseInt(res.get(0).toString());
	}

}
